<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>params</title>
</head>
<body>
<input type="text" name="url" size="100" id="url" value="http://www.megavideo.com/dv.php?s=ddd&v=YUCLC1BU##000s" />
<br />
<input type="text" name="param" id="param" />
<input type="button" value="提交" onclick="showParam();" />
<script type="text/javascript">
function urlparse(url){
    var result = {};
	var index;
	//protocol://username:password@domain[:port]/path[?query]#fragment
	
	//alert(window.location.hash.substr(1))
	
	index = url.indexOf('#');
	if (index != -1) {
		var fragment = url.substring(index + 1);
		if (fragment) {
			result["#"] = result["fragment"] = fragment;
		}
		url = url.substring(0, index);
	}
	index = url.indexOf('?');
	if (index != -1) {
		var query = url.substring(index + 1);
		if (query) {
			var params = {};
			var paramlist = query.split("&");
			for (var i in paramlist) {
				var param = paramlist[i]
				var pindex = param.indexOf("=");
				var pname,pvalue;
				if (pindex != -1) {
					pname = param.substring(0, pindex);
					pvalue = param.substring(pindex + 1);
				} else {
					pname = param;
					pvalue = "";
				}
				params[pname] = decodeURIComponent(pvalue);
			}
			result["?"] = result["query"] = params;
		}
		url = url.substring(0, index);
	}
	index = url.indexOf(':');
	if (index != -1) {
		var protocol = url.substring(0, index);
		result["protocol"] = protocol.toLowerCase();
		url = url.substring(index + 1);
	}
	if (url.substr(0, 2) == "//") {
		url = url.substring(2);	
	}
	index = url.indexOf('/');
	if (index != -1) {
		result["path"] = url.substring(index + 1);
		url = url.substring(0, index);
	}
	index = url.indexOf('@');
	if (index != -1) {
		var passport = url.substring(0, index).split(":");
		result["username"] = passport[0];
		result["password"] = passport[1];
		url = url.substring(index + 1);
	}
	index = url.indexOf(':');
	if (index != -1) {
		result["port"] = url.substring(index + 1);
		url = url.substring(0, index);
	}
	result["domain"] = result["ip"] = url.toLowerCase();
    return result;
}


function showParam() {
	var params = urlparse(document.getElementById("url").value);
	var pname = document.getElementById("param").value;
	var query = params["?"];
	if (pname) {
		alert(params[pname]);
	}
}


</script>
<div id="output"></div>
</body>
</html>
